/**
 * Created by wm123 on 2017/6/26.
 */
const { GameServer } = require('board-game');
const { server } = require('./config');
const GuessDrawGame = require('./game');
const GuessDrawRole = require('./role');
const GuessDrawBoard = require('./board');

const watchCanvas = (watch, game) => game && watch(game.canvas, 'canvas');
const appendLines = (append, game) => game && append(game.canvas.lines, 'lines');

let find = questions => {
    if (Array.isArray(questions)) {
        return questions.length;
    } else {
        return Object.values(questions).reduce((s, v) => s + find(v), 0);
    }
};
console.log(`题库中已拥有${ find(require('./questions')) }道题目`);

GameServer.start({
    drawDown: ({ data, role }) => role.drawDown(data),
    drawUp: ({ data, role }) => role.drawUp(data),
    drawMove: ({ data, role }) => role.drawMove(data),
    drawMoves: ({ data, role }) => role.drawMoves(data),
    undo: ({ role }) => role.undo(),
    empty: ({ role }) => role.empty(),
    good: ({ role }) => role.good(),
    bad: ({ role }) => role.bad(),
    setColor: ({ data, role }) => role.setColor(data),
    setSize: ({ data, role }) => role.setSize(data),
    login: [
        ({ watch, game }) => watchCanvas(watch, game),
        ({ append, game }) => appendLines(append, game)
    ],
    ready: [
        ({ watch, game }) => watchCanvas(watch, game),
        ({ append, game }) => appendLines(append, game)
    ]
}, {
    Game: GuessDrawGame,
    Role: GuessDrawRole,
    Board: GuessDrawBoard
}, server, {
    index: './web/index.js',
    compress: process.env.NODE_ENV !== 'development',
    watch: process.env.NODE_ENV === 'development',
    stats: 'verbose'
});